﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.IO;
using ManagedDLL;

namespace ServerApp
{
    public partial class MainForm : Form
    {
        private TcpListener listener;
        private Thread listenerThread;

        public delegate void UpdateTextCallback(string message);
        public delegate void UpdatePlayersListCallback(string datapackageobject, int addorremove);

        private List<Client> clientsonline;

        public MainForm()
        {
            InitializeComponent();
            ClientManager.Instance.form = this;
            this.stopServerToolStripMenuItem.Enabled = false;
            this.ServerStatusStripLabel.ForeColor = Color.Red;
            clientsonline = new List<Client>();
        }

        private void startServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");

            listener = new TcpListener(localAddr, 1234);
            listenerThread = new Thread(this.ListenToClients);
            listenerThread.Start();

            this.startServerToolStripMenuItem.Enabled = false;
            this.stopServerToolStripMenuItem.Enabled = true;
            this.ServerStatusStripLabel.Text = "Status: Online";
            this.ServerStatusStripLabel.ForeColor = Color.Green;
        }

        private void stopServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listener.Stop();
            listenerThread.Abort();
            this.startServerToolStripMenuItem.Enabled = true;
            this.stopServerToolStripMenuItem.Enabled = false;
            this.ServerStatusStripLabel.ForeColor = Color.Red;
            this.ServerStatusStripLabel.Text = "Status: Offline";
        }

        public void ListenToClients()
        {
            listener.Start();
            while (true)
            {
                TcpClient client = this.listener.AcceptTcpClient();
                Client c = new Client(client);
                Thread t = new Thread(c.HandleClientConnection);
                t.Start();                
            }
        }

        public void UpdatePlayersList(string datapackageobjectname, int addorremove)
        {
            if(addorremove == 0)
                PlayersOnlineListBox.Items.Add(datapackageobjectname);
            else
                PlayersOnlineListBox.Items.Remove(datapackageobjectname);
        }

        public void UpdateText(string message)
        {
            ConsoleTextBox.Text += message + "\r\n";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ClientManager.Instance.SendAllClientsChatMessage(sendTextBox.Text, "Console");            
        }

        private void registerToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
